package com.kha.listone;

import java.util.Stack;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.kha.listone.common.CompoundScreen;
import com.kha.listone.common.SingleScreen;
import com.kha.listone.common.model.SettingsModel;
import com.kha.listone.common.model.TaskModel;
import com.kha.listone.locale.LocaleConstants;
import com.kha.listone.locale.LocaleResources;
import com.kha.listone.screen.compound.FilterScreen;
import com.kha.listone.screen.compound.ListScreen;
import com.kha.listone.screen.compound.TaskScreen;
import com.kha.listone.screen.single.AboutScreen;
import com.kha.listone.screen.single.AddTaskScreen;
import com.kha.listone.screen.single.DetailScreen;
import com.kha.listone.screen.single.SettingScreen;
import com.kha.listone.util.Commands;
import com.kha.listone.util.Globals;
import com.kha.listone.util.ImageLoader;
import com.kha.listone.util.operation.Operation;
import com.kha.listone.util.operation.OperationsQueue;
import com.kha.listone.util.xml.SettingsXMLParser;
import com.nokia.mid.ui.CategoryBar;
import com.nokia.mid.ui.ElementListener;
import com.nokia.mid.ui.IconCommand;

public class ListOne extends MIDlet implements CommandListener, ElementListener {

	private Displayable current;
	private Stack stack;

	private AboutScreen aboutScreen;
	private SettingScreen settingScreen;
	private AddTaskScreen addTaskScreen;
	private DetailScreen detailScreen;

	private TaskScreen taskScreen;
	private ListScreen listScreen;
	private FilterScreen filterScreen;

	private IconCommand tabTask;
	private IconCommand tabList;
	private IconCommand tabFilter;

	private CategoryBar tabBar;

	private OperationsQueue enqueueOperation;

	public ListOne() {
		super();		
		this.stack = new Stack();		
		enqueueOperation = new OperationsQueue();
		enqueueOperation.enqueue(new SettingsXMLParser());
		enqueueOperation.enqueue(new PostInit(this));
	}

	protected void destroyApp(boolean unconditional)
			throws MIDletStateChangeException {
	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {
		enqueueOperation.start();
	}

	public Displayable getAboutScreen() {
		if (aboutScreen == null) {
			aboutScreen = new AboutScreen(this, LocaleResources.getInstance()
					.getString(LocaleConstants.keys.ABOUT_OPTION_MENU));
		} else {
			aboutScreen.refresh();
		}
		return aboutScreen;
	}

	public Displayable getSettingScreen() {
		if (settingScreen == null) {
			settingScreen = new SettingScreen(this,
					LocaleConstants.locale.SETTING_OPTION_MENU);
		} else {
			settingScreen.refresh();
		}
		return settingScreen;
	}

	public Displayable getTaskScreen() {
		if (taskScreen == null) {
			taskScreen = new TaskScreen(this,
					LocaleConstants.locale.TASK_SCREEN_TITLE, List.IMPLICIT);
			taskScreen.setTabBar(tabBar);
		} else {			
			taskScreen.refresh();
		}
		return taskScreen;
	}

	public Displayable getListScreen() {
		if (listScreen == null) {
			listScreen = new ListScreen(this,
					LocaleConstants.locale.LIST_SCREEN_TITLE, List.IMPLICIT);
			listScreen.setTabBar(tabBar);
		} else {
			listScreen.refresh();
		}
		return listScreen;
	}

	public Displayable getFilterScreen() {
		if (filterScreen == null) {
			filterScreen = new FilterScreen(this,
					LocaleConstants.locale.FILTER_SCREEN_TITLE, List.IMPLICIT);
			filterScreen.setTabBar(tabBar);
		} else {
			filterScreen.refresh();
		}
		return filterScreen;
	}

	public Displayable getAddTaskScreen() {
		if (addTaskScreen == null) {
			addTaskScreen = new AddTaskScreen(this,
					LocaleConstants.locale.ADD_TASK_SCREEN_LABEL);
		} else {
			addTaskScreen.refresh();
		}
		return addTaskScreen;
	}

	public Displayable getDetailScreen(TaskModel model) {
		if (detailScreen == null) {
			detailScreen = new DetailScreen(this,
					LocaleConstants.locale.DETAIL_SCREEN_TITLE, model);
		} else {
			detailScreen.refresh(model);
		}
		return detailScreen;
	}

	public void next(Displayable next) {
		if (this.current == next) {
			return;
		}

		if (this.current != null) {
			this.stack.push(this.current);
			if (stack.size() > Globals.MAX_STACK) {
				stack.removeElementAt(0);
				stack.trimToSize();
			}
		}

		if (next.getClass() == TaskScreen.class
				|| next.getClass() == ListScreen.class
				|| next.getClass() == FilterScreen.class) {
			getTabBar().setVisibility(true);
		} else {
			getTabBar().setVisibility(false);
		}

		this.current = next;
		Display.getDisplay(this).setCurrent(this.current);

		// Show hide Category bar

	}

	public void back() {
		if (this.stack.size() > 0x00) {
			this.current = (Displayable) this.stack.pop();
			stack.trimToSize();
			showDisplay(current);
			if (current.getClass() == TaskScreen.class
					|| current.getClass() == ListScreen.class
					|| current.getClass() == FilterScreen.class) {
				getTabBar().setVisibility(true);
				((CompoundScreen) current).refresh();
			} else {
				getTabBar().setVisibility(false);
				((SingleScreen) current).refresh();
			}
			if (current.getClass() == TaskScreen.class) {
				getTabBar().setSelectedIndex(0);
			}
			if (current.getClass() == ListScreen.class) {
				getTabBar().setSelectedIndex(1);
			}
			if (current.getClass() == FilterScreen.class) {
				getTabBar().setSelectedIndex(2);
			}

		} else {
			Alert alert = new Alert("ListOne", "Are you sure?",
					ImageLoader.load(ImageLoader.PICTURE), AlertType.WARNING);

			alert.addCommand(Commands.ALERT_OK);
			alert.addCommand(Commands.ALERT_CANCEL);
			alert.setCommandListener(this);
			showDisplay(alert);
		}
	}

	public void commandAction(Command c, Displayable d) {
		if (c == Commands.ALERT_OK) {
			this.notifyDestroyed();
		}

		if (c == Commands.ALERT_CANCEL) {
			showDisplay(current);
		}
	}

	public void showDisplay(Displayable d) {
		Display.getDisplay(this).setCurrent(d);
	}

	public CategoryBar getTabBar() {
		return tabBar;
	}

	public void setTabBar(CategoryBar tabBar) {
		this.tabBar = tabBar;
	}

	public void notifyElementSelected(CategoryBar bar, int selectedIndex) {
		switch (selectedIndex) {
		case CompoundScreen.TASK_ID:
			this.next(this.getTaskScreen());
			break;
		case CompoundScreen.LIST_ID:
			this.next(this.getListScreen());
			break;
		case CompoundScreen.FILTER_ID:
			// this.next(this.getFilterScreen());
			if (current.getClass() == TaskScreen.class) {
				this.next(this.getAddTaskScreen());
			}
			break;
		case ElementListener.BACK:
			this.back();
			break;
		default:
			this.next(this.getTaskScreen());
			break;
		}
	}

	private class PostInit implements Operation {

		private ListOne midlet;

		public PostInit(ListOne midlet) {
			this.midlet = midlet;
		}

		public void execute() {

			synchronized (midlet) {

				Commands.refresh();

				// Category Tabs
				tabTask = new IconCommand("Tasks",
						ImageLoader.load(ImageLoader.CATEGORY_1), null,
						Command.SCREEN, 1);
				tabList = new IconCommand("Lists",
						ImageLoader.load(ImageLoader.ADD), null,
						Command.SCREEN, 1);
				tabFilter = new IconCommand("Filter",
						ImageLoader.load(ImageLoader.CATEGORY_LIST), null,
						Command.SCREEN, 2);

				IconCommand[] iconCommands = { tabTask, tabFilter, tabList };
				// Category Tabs

				tabBar = new CategoryBar(iconCommands, true);
				getTabBar().setElementListener(midlet);

				getTabBar().setVisibility(true);

				next(getTaskScreen());

			}

		}

	}
}
